<?php

/**
 *  后台继承类
 * @file   Admin.php  
 * @date   2016-8-23 19:45:21 
 * @author Zhenxun Du<5552123@qq.com>  
 * @version    SVN:$Id:$ 
 */
namespace app\admin\controller;

use think\Loader;

class Warehouse extends Common {

    public function _initialize(){

        $this->standby = new \app\admin\model\Standby();
        $this->securitie = new \app\admin\model\Securities();
        $this->warehouse = new \app\admin\model\Warehouse();
    }

    //ROE20管理
    public function index() {

        $list = null;
        $list = $this->securitie->securitieList(['status'=>4]);
        foreach($list AS $k=>$v){
            if($v['sid']==null){
                unset($list[$k]);
            }else{
                $warehouse = null;
                $warehouse = $this->warehouse->where(['sid'=>$v['sid']])->find();

                if($warehouse != null){
                    $list[$k]['buy_price'] = $warehouse->buy_price;
                    $list[$k]['buy_sell_price'] = $warehouse->buy_sell_price;
                    $list[$k]['profit_margin'] =  sprintf("%.2f",substr(sprintf("%.4f", $v['price']/$warehouse->buy_price), 0, -2));
                    $list[$k]['rote'] = $warehouse->rote;

                    if($list[$k]['profit_margin']>10){
                        $list[$k]['profit_margin_bj'] = '#FF0000';
                    }elseif($list[$k]['profit_margin']>6){
                        $list[$k]['profit_margin_bj'] = '#FF5C5C';
                    }elseif($list[$k]['profit_margin']>5){
                        $list[$k]['profit_margin_bj'] = '#FF5C95';
                    }elseif($list[$k]['profit_margin']>3){
                        $list[$k]['profit_margin_bj'] = '#EA5505';
                    }elseif($list[$k]['profit_margin']<1){
                        $list[$k]['profit_margin_bj'] = '#02D76E';
                    }
                }
            }
        }
        $this->assign('lists', $list);
        return $this->fetch();
    }

    //编辑页
    public function editp(){

        $data = input();

        if ($data['sid'] && $data['stock_code']) {

            $baseInfo = [];
            $stockInfo = $this->securitie->where(['sid'=>$data['sid']])->find();
            $baseInfo['sid'] = $stockInfo->sid;
            $baseInfo['stock_name'] = $stockInfo->stock_name;
            $baseInfo['stock_code'] = $stockInfo->stock_code;

            $baseInfo['buy_price'] = 0.00;
            $baseInfo['buy_sell_price'] = 0.00;
            $baseInfo['buy_status'] = 0;
            $baseInfo['rote'] = '';
            $warehouse = $this->warehouse->where(['sid'=>$data['sid']])->find();
            if($warehouse != null){
                $baseInfo['buy_price'] = $warehouse->buy_price;
                $baseInfo['buy_sell_price'] = $warehouse->buy_sell_price;
                $baseInfo['buy_status'] = $warehouse->buy_status;
                $baseInfo['rote'] = $warehouse->rote;
            }
            $this->assign('detail', $baseInfo);
        }
        return $this->fetch();
    }

    public function edit(){

        $data = input();
        if ($data['sid'] && $data['stock_code']) {

            $arr = [];
            if($data['buy_price'] > 0){
                $arr['buy_price'] = $data['buy_price'];
            }
            if($data['buy_sell_price'] > 0){
                $arr['buy_sell_price'] = $data['buy_sell_price'];
            }
            $arr['buy_status'] = $data['buy_status'];
            $arr['rote'] = $data['rote'];

            $warehouse = $this->warehouse->where(['sid'=>$data['sid']])->find();
            if($warehouse != null){
                $this->warehouse->where(['sid'=>$data['sid']])->update($arr);
            }else{
                $arr['sid'] = $data['sid'];
                $arr['buy_time'] = time();

                $this->warehouse->insert($arr); 
            }
            $this->success('操作成功', url('index'));
        }
    }

    //删除
    public function del() {

        $data = input();
        $this->securitie->securitieInfoUpdate($data['sid'], $data['stock_code'],['status'=>$data['status']]);
        $this->success('操作成功', url('index'));
    }
}